import { useEffect, useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import "../index.less";
import HomeItem from "../homeitem";
import { getWeatherInfo } from "../service";
// 活动报名
const firstRow = [
    {
        label: "社区服务",
        imgs: "../../images/hdbm0.png",
        url: "/communityService",
    },
    {
        label: "社区活动",
        imgs: "../../images/hdbm1.png",
        url: "/communityActivities?type=activities",
    },
    {
        label: "就业引导",
        imgs: "../../images/hdbm2.png",
        url: "/communityActivities?type=guidance",
    },
    {
        label: "创业扶持",
        imgs: "../../images/hdbm3.png",
        url: "/communityActivities?type=support",
    },
];
// 共建共治
const secondRow = [
    {
        label: "随手拍",
        imgs: "../../images/gjgz0.png",
        url: "/eventReport",
    },
    {
        label: "社区回音壁",
        imgs: "../../images/gjgz1.png",
        url: "/intelligentGovernance/wallOfEchoes",
    },
    {
        label: "社区议事",
        imgs: "../../images/gjgz2.png",
        url: "/intelligentGovernance/theTownHall",
    },
    {
        label: "社区问卷",
        imgs: "../../images/gjgz3.png",
        url: "/intelligentGovernance/communityQuestionnaire",
    },
];
// 咨询共享
const thirdRow = [
    {
        label: "社区资讯",
        imgs: "../../images/zxgx0.png",
        url: "/communityInformation?type=info",
    },
    {
        label: "政策解读",
        imgs: "../../images/zxgx1.png",
        url: "/communityInformation?type=unscramble",
    },
    {
        label: "办事指南",
        imgs: "../../images/zxgx2.png",
        url: "/communityInformation?type=guide",
    },
    {
        label: "普法宣传",
        imgs: "../../images/zxgx3.png",
        url: "/communityInformation?type=disseminate",
    },
];
const HomePage: React.FC = () => {
    const location = useLocation();
    const navigate = useNavigate();
    const [weather, setWeather] = useState<any>()

    const goto = (url: string) => () => {
        navigate(url);
        // 获取要滚动的元素
        let element = document.getElementsByClassName('homePage-contarnier')[0];
        // 获取当前页面滚动的高度
        let scrollTop = element.scrollTop.toString();
        sessionStorage.setItem('scrollTop', scrollTop);
    };
    useEffect(() => {
        // 在其他页面加载完成后，从 sessionStorage 中获取保存的滚动高度
        if (sessionStorage.getItem('scrollTop')) {
            // 将保存的滚动高度设置为当前页面的滚动高度
            let element = document.getElementsByClassName('homePage-contarnier')[0];
            element.scrollTo(0, Number(sessionStorage.getItem('scrollTop')));
            // 清除 sessionStorage 中的滚动高度
            sessionStorage.removeItem('scrollTop');
        }
        // 查询用户个人信息
        getWeatherInfo({ villageCode: "511602006004" })
            .then((res: any) => {
                if (res && res.code == '200') {
                    try {
                        const realdata = JSON.parse(res.data)
                        console.log(realdata);

                        if (realdata && realdata.data) {
                            const data = realdata.data?.real
                            if (data && data.weather) {
                                setWeather(data.weather)
                                //   console.log(data.weather);
                            }
                        }
                    } catch (e) {
                        console.info('天气信息错误')
                    }
                }
            })
            .catch((error) => {
                console.log(error);
            });
    }, [])
    return (
        <div className="content">
            <div className="imgBox">
                <img src="/images/zhdj.png" alt="" />
            </div>
            <div className="weath">
                <span>广安区</span>
                <span>晴 {weather?.temperatureDiff}℃/{weather?.temperature}℃</span>
                {/* <span>今日尾号限行：3/8</span> */}
            </div>
            <div className="itemBox">
                <div className="firstRow">
                    <div className="community" onClick={goto("/myCommunity")}>
                        <div className="title">我的社区</div>
                        <div className="intorduce">社区简介 | 组织架构</div>
                    </div>
                    <div className="party" onClick={goto("/smartPartyBuilding")}>
                        <div className="title">智慧党建</div>
                        <div className="intorduce">党建活动 | 党建资讯</div>
                    </div>
                </div>
                <div className="secondRow">
                    <div
                        className="governance"
                        onClick={goto("/intelligentGovernance")}
                    >
                        <div className="title">智慧治理</div>
                        <div className="intorduce">随手“拍” | 投诉建议</div>
                    </div>
                    <div className="life" onClick={goto("/lifeCircle")}>
                        <div className="title">15分钟生活圈</div>
                        <div className="intorduce">社区云导览 | 网点查询</div>
                    </div>
                </div>
            </div>
            <HomeItem title="便民创新" itemList={firstRow} />
            <HomeItem title="共建共治" itemList={secondRow} />
            <HomeItem title="资讯共享" itemList={thirdRow} />
        </div>
    );
};

export default HomePage;
